# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  # Pick a unique cookie name to distinguish our session data from others'
  session :session_key => '_redafonrails_session_id'
  
  # Define o charset default da aplicação
  def configure_charsets(charset='UTF-8')
    suppress(ActiveRecord::StatementInvalid) do
      ActiveRecord::Base.connection.execute "SET NAMES 'UTF8'"
    end
  end
end

# paginate by sql
# http://thebogles.com/blog/2006/06/paginate_by_sql-for-rails-a-more-general-approach/
# added support for sql with arguments
# added a :count option for passing in either a Integer count or count query.
module ActiveRecord
  class Base
    def self.find_by_sql_with_limit(sql, offset, limit)
      sql = sanitize_sql(sql)
      add_limit!(sql, {:limit => limit, :offset => offset})
      find_by_sql(sql)
    end
    
    def self.count_by_sql_wrapping_select_query(sql)
      sql = sanitize_sql(sql)
      count_by_sql("select count(*) from (#{sql}) as x")
    end
  end
end

class ApplicationController < ActionController::Base
  def paginate_by_sql(model, sql, per_page, options={})
    if options[:count]
      if options[:count].is_a? Integer
        total = options[:count]
      else
        total = model.count_by_sql(options[:count])
      end
    else
      total = model.count_by_sql_wrapping_select_query(sql)
    end
    
    object_pages = Paginator.new self, total, per_page, params['page']
    objects = model.find_by_sql_with_limit(sql,
                                           object_pages.current.to_sql[1], per_page)
    return [object_pages, objects]
  end
  
  # filtra os eventos por perfil
  def filtra_eventos(eventos)
    #### Ações permitidas a cada perfil #########
    analista = ["enviarParaDesenvolvimento",
                "enviarParaAtualizacaoProducao", 
                "enviarParaCertificacao",
                "reprovarDesenvolvimentoAnalista",
                "aprovarDesenvolvimentoAnalista",
                "solicitarAtualizacaoServidorProd",
                "solicitarAtualizacaoBdProd"]
    desenvolvedor = ["iniciarDesenvolvimento",
                     "suspenderDesenvolvimento",
                     "concluirDesenvolvimento"]
    certificador = ["iniciarCertificacao",
                    "reprovarRequisicao",
                    "solicitarAtualizacaoServidorCert",
                    "solicitarAtualizacaoBdCert",
                    "aprovarRequisicao",
                    "suspenderCertificacao"]
    pool = ["escalarDesenvolvedor",
            "reprovarDesenvolvimento",
            "aprovarDesenvolvimento",
            "iniciarDesenvolvimento",
            "suspenderDesenvolvimento",
            "concluirDesenvolvimento"]
    
    certificacao = ["escalarCertificador",
                  "solicitarAtualizacaoBdCert",
                  "cancelarCert",
                  "solicitarAtualizacaoServidorCert"]
    gerente  = ["autorizarAtualizacao"]
    banco = ["cancelarAtualizacaoBdCert",
            "iniciarAtualizacaoBdCert",
            "suspenderAtualizacaoBdCert",
            "concluirAtualizacaoBdCert",
            "iniciarAtualizacaoBdProd",
            "suspenderAtualizacaoBdProd",
            "cancelarAtualizacaoBdProd",
            "concluirAtulizacaoBdProd"]
    suporte = ["iniciarAtualizacaoServidorCert",
              "cancelarAtualizacaoServidorCert",
              "suspenderAtualizacaoServidorCert",
              "concluirAtualizacaoServidorCert",
              "iniciarAtualizacaoServidorProd",
              "suspenderAtualizacaoServidorProd",
              "cancelarAtualizacaoServidorProd",
              "concluirAtulizacaoServidorProd"]
    producao = ["solicitarAtualizacaoBdProd",
            "solicitarAtualizacaoServidorProd",
            "reportarErroProducao",
            "autorizaEnvioProd"]
    
    tecnico = ["solicitarAtualizacaoBdProd",
            "solicitarAtualizacaoServidorProd",
            "reportarErroProducao"]   
    #############################################  
    ret = []
    user = Goldberg.user.role_id  
    if Goldberg.user.role_id == 4
      for e in eventos
        analista.each do |a|
          if a == e.to_s
            ret << e
          end
        end
      end
    elsif Goldberg.user.role_id == 10
      for e in eventos
        certificador.each do |a|
          if a == e.to_s
            ret << e
          end
        end
      end
      
    elsif Goldberg.user.role_id == 14 
      for e in eventos
        pool.each do |a|
          if a == e.to_s
            ret << e
          end
        end
      end
    elsif Goldberg.user.role_id == 5 
      for e in eventos
        desenvolvedor.each do |a|
          if a == e.to_s
            ret << e
          end
        end
      end
    elsif Goldberg.user.role_id == 13 
      for e in eventos
        certificacao.each do |a|
          if a == e.to_s
            ret << e
          end
        end
      end
    elsif Goldberg.user.role_id == 11 
      for e in eventos
        banco.each do |a|
          if a == e.to_s
            ret << e
          end
        end
      end
    elsif Goldberg.user.role_id ==12 
      for e in eventos
        suporte.each do |a|
          if a == e.to_s
            ret << e
          end
        end
      end
    elsif Goldberg.user.role_id == 8 
      for e in eventos
        producao.each do |a|
          if a == e.to_s
            ret << e
          end
        end
      end
    elsif Goldberg.user.role_id == 9
      for e in eventos
        tecnico.each do |a|
          if a == e.to_s
            ret << e
          end
        end
      end 
    elsif Goldberg.user.role_id == 7 
      for e in eventos
        gerente.each do |a|
          if a == e.to_s
            ret << e
          end
        end
      end  
    end
    if ret.empty?
      return ""
    else
      return ret
    end
  end 

end
